package thread;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-08-13
 * Time: 20:32
 */
public class demo10 {
    public static void main(String[] args) {
        Thread t = new Thread(()->{
            Thread cur = Thread.currentThread(); //获取当前线程的引用
            while(!cur.isInterrupted()){    //Thread类内部设置了一个boolean变量
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    // 执行一些其他逻辑~~
                    // 退出之前做一些释放资源类工作
                   // break;

                }
            }
        });
        t.start();
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入 0 表示让线程 t 结束: ");
        int n = scanner.nextInt();
        if(n == 0){
            t.interrupt();  //手动修改标志为为ture, 但是这里会唤醒sleep, 但是sleep被提前唤醒后, 会把标志位又设置成true
        }
    }
}
